<template>
  <div class="user clearfix">
    <div class="common-level-rail fl">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="addClick" v-auth="'/supplier/supplier/add'">
        添加区域代理
      </el-button>
    </div>
    <div class="common-seach-wrap fr">
      <el-form size="small" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input v-model="formInline.search" placeholder="请输入代理名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="proxy_id" label="代理ID" width="90"></el-table-column>
          <el-table-column prop="name" label="代理名称" width="130"></el-table-column>
          <el-table-column prop="real_name" label="真实姓名" width="130"></el-table-column>
          <el-table-column prop="user_name" label="登录账号" width="130"></el-table-column>
          <el-table-column prop="mobile" label="手机号" width="130"></el-table-column>
          <el-table-column label="区域">
            <template slot-scope="scope">
              <div class="region-box" v-html="showRegion(scope.row.regions)"></div>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="添加时间" width="150"></el-table-column>
          <el-table-column label="启用" width="80" align="center">
            <template slot-scope="{row}">
              <el-switch
                :handleBeforeChange="()=>changeStatus(row)"
                active-color="#13ce66"
                v-model="row.status"
                v-loading="row.loading"
                :active-value="1"
                :inactive-value="0"
                inactive-color="#dadbdf"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <template slot-scope="scope">
              <el-button @click="editClick(scope.row)" type="text" size="small" v-auth="'/proxy/proxy/edit'">
                编辑
              </el-button>
              <el-button @click="deleteClick(scope.row)" type="text" size="small" v-auth="'/proxy/proxy/delete'">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--分页-->
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
                     :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
                     :total="totalDataNumber"></el-pagination>
    </div>
  </div>
</template>
<script>
import ProxyApi from '@/api/proxy.js';
import RegionApi from '@/api/region.js';

export default {
  components: {},
  data() {
    return {
      /*是否加载完成*/
      loading: false,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        shop_id: '',
        search: ''
      },
      allMergerNames: {},
    }
  },
  created() {
    this.getMergerNames()
    /*获取列表*/
    this.getTableList();
  },
  methods: {

    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    onSubmit() {
      this.curPage = 1;
      this.getTableList();
    },
    showRegion(regions) {
      let self = this;
      let html = "";
      if (regions instanceof Array) {
        regions.forEach(item => {
          html += "<div class='region-item'>" + self.allMergerNames[item.region_id] + "</div>";
        })
      }
      return html;
    },

    getMergerNames() {
      let self = this;
      RegionApi.getMergerNames().then(res => {
        self.allMergerNames = res.data.allMergerNames;
      }).catch(error => {
      });
    },
    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = this.formInline;
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      Params.list_rows = self.pageSize;
      ProxyApi.proxyList(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.list.data;
        self.totalDataNumber = res.data.list.total;
      }).catch(error => {
        self.loading = false;
      });
    },
    /*打开添加*/
    addClick() {
      this.$router.push('/proxy/proxy/add');
    },
    editClick(row) {
      let params = row.proxy_id;
      this.$router.push({
        path: '/proxy/proxy/edit',
        query: {
          proxy_id: params
        }
      })
    },
    changeStatus(row) {
      let self = this;
      return new Promise(resolve => {
        let war = row.status == 1 ? "禁止" : "启用";
        self.$confirm("确认要" + war + "吗?", '提示', {
          type: 'warning'
        }).then(() => {
          row.loading = true;
          ProxyApi.proxyRecycle({
            proxy_id: row.proxy_id,
            status: row.status==1?0:1
          }).then(data => {
            self.$message({
              message: war + '成功',
              type: 'success'
            });
            row.loading = false;
            resolve(true);
          }).catch(() => {
            row.loading = false;
            resolve(false)
          })
        }).catch(() => {
          row.loading = false;
          resolve(false)
        })
      })
    },
    deleteClick(row) {
      let self = this;
      self.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.loading = true;
        ProxyApi.deleteProxy({
          proxy_id: row.proxy_id
        }, true)
          .then(data => {
            self.loading = false;
            if (data.code == 1) {
              self.$message({
                message: '恭喜你，删除成功',
                type: 'success'
              });
              self.getTableList();
            }
          })
          .catch(error => {
            self.loading = false;
          });

      }).catch(() => {
        self.loading = false;
      });
    }
  }
}
</script>

<style scoped>
.region-box >>> .region-item {
  display: inline-block;
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 1px 8px;
  margin-right: 5px;
  margin-top: 2px;
  margin-bottom: 2px;
}
</style>
